package com.tengju.support.infrastructure.repository.message;

import com.tengju.support.domain.model.message.*;
import com.tengju.support.infrastructure.persistence.mybatis.dataobject.SupportMessageContentDO;
import com.tengju.support.infrastructure.persistence.mybatis.mapper.tengju.custom.SupportMessageTaskMapper;
import com.tengju.support.infrastructure.persistence.redis.MessageCacheRepository;
import com.tengju.support.infrastructure.repository.message.convert.MessageContentConvert;
import lombok.RequiredArgsConstructor;
import org.redisson.api.RLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Repository
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class MessageTaskRepositoryImpl implements MessageTaskRepository {

    private final SupportMessageTaskMapper messageTaskMapper;
    private final MessageCacheRepository messageCacheRepository;

    @Override
    public List<MessageTask> getBySendStatusLessSendTime(SendStatus sendStatus, LocalDateTime sendTime) {
        List<SupportMessageContentDO> supportMessageContentDOList =
                messageTaskMapper.queryBySendStatusLessSendTime(sendStatus.getVal(), sendTime);
        return supportMessageContentDOList.stream()
                .map(MessageContentConvert::convertToModel).collect(Collectors.toList());
    }

    @Override
    public List<MessageTask> getBySendStatus(SendStatus sendStatus) {
        List<SupportMessageContentDO> supportMessageContentDOList =
                messageTaskMapper.queryBySendStatus(sendStatus.getVal());
        return supportMessageContentDOList.stream()
                .map(MessageContentConvert::convertToModel).collect(Collectors.toList());
    }

    @Override
    public TaskId save(MessageTask messageContent) {
        SupportMessageContentDO supportMessageContentDO = MessageContentConvert.convertToDO(messageContent);
        messageTaskMapper.insertSelective(supportMessageContentDO);
        return new TaskId(supportMessageContentDO.getId());
    }

    @Override
    public MessageTask get(TaskId messageId) {
        SupportMessageContentDO supportMessageContentDO = messageTaskMapper.selectByPrimaryKey(messageId.getValue());
        return MessageContentConvert.convertToModel(supportMessageContentDO);
    }

    @Override
    public void edit(MessageTask messageContent) {
        SupportMessageContentDO supportMessageContentDO = MessageContentConvert.convertToDO(messageContent);
        messageTaskMapper.updateByPrimaryKeySelective(supportMessageContentDO);
    }

    @Override
    public void remove(TaskId messageId, String updateBy) {
        messageTaskMapper.deleteByPrimaryKey(messageId.getValue(), updateBy);
    }

    @Override
    public int getPageTotal(String keyword, List<MessageType> messageTypeList) {
        List<Integer> messageTypeValList = messageTypeList.stream()
                .map(MessageType::getVal)
                .collect(Collectors.toList());
        return messageTaskMapper.selectPageTotal(keyword, messageTypeValList);
    }

    @Override
    public List<MessageTask> getPage(String keyword, List<MessageType> messageTypeList, Integer offset, Integer limit) {
        List<Integer> messageTypeValList = messageTypeList.stream()
                .map(MessageType::getVal)
                .collect(Collectors.toList());
        List<SupportMessageContentDO> supportMessageContentDOList = messageTaskMapper.selectPage(keyword, messageTypeValList, offset, limit);
        return supportMessageContentDOList.stream()
                .map(MessageContentConvert::convertToModel)
                .collect(Collectors.toList());
    }

    @Override
    public RLock getBatchInitLock(TaskId messageId) {
        return messageCacheRepository.getMessageSendLock(messageId);
    }

    @Override
    public boolean updateSendStatus(TaskId messageId, SendStatus beforeStatus, SendStatus afterStatus) {
        return messageTaskMapper
                .updateSendStatus(messageId.getValue(),beforeStatus.getVal(),afterStatus.getVal()) == 1;
    }
}
